//created by: Lim Yi Le
Ext.define('MajorProjectSenchaTouch.controller.ContactUs', {
    extend: 'Ext.app.Controller',
    config:{
        views: ['ContactUsModule.RecommendTitle', 'ShelfLocator'],
        refs:{
            recommendTitleButton: 'button[action=goRecommendTitle]',
            callPhoneButton: 'button[action=callPhone]',
            emailButton: 'button[action=email]',
            submitToEmailButton: 'button[action=submitToEmail]',
            openLibraryBlogButton: 'button[action=openLibraryBlog]',
            openLibraryFacebookButton: 'button[action=openLibraryFacebook]',
            rtCloseBtn: '#rtCloseBtn',
            contactUsPage: 'contactuspage',
            recommendTitleOverlay: 'recommendtitleoverlay',
            navView: 'mainNavView'
        },
        control:{
            recommendTitleButton:{
                tap: 'goRecommendTitleOverlay'
            },
            rtCloseBtn:{
                tap: 'closeRecommendTitleOverlay'
            },
            callPhoneButton:{
                tap: 'goCallPhone'
            },
            emailButton:{
                tap: 'goEmail'
            },
            openLibraryBlogButton:{
                tap: 'goOpenLibraryBlog'
            },
            openLibraryFacebookButton:{
                tap: 'goOpenLibraryFacebook'
            },
            submitToEmailButton:{
                tap: 'goSubmitToEmail'
            }
        }
    },
    /*
|--------------------------------------------------------------------------
| goRecommendTitleOverlay (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function is triggered by tapping on the RecommendTitle button in the ContactUs view
| and pops up the RecommendTitle overlay form.
|
*/
    goRecommendTitleOverlay: function(){
        var rtOverlay = this.getRecommendTitleOverlay();
        if(rtOverlay == undefined){
            rtOverlay = Ext.create('MajorProjectSenchaTouch.view.ContactUsModule.RecommendTitle');
            Ext.Viewport.add(rtOverlay);         
        }
        else{
            rtOverlay.show();
        }
    },
    /*
|--------------------------------------------------------------------------
| closeRecommendTitleOverlay (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function is triggered by tapping on the cancel button on the Recommend Title
| overlay to close it.
|    
*/
    closeRecommendTitleOverlay: function(){
        var rtOverlay = this.getRecommendTitleOverlay();
        rtOverlay.hide();
    },
    /*
|--------------------------------------------------------------------------
| goCallPhone (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function is triggered by tapping on the phone button on the ContactUs view
| to access the native phone call function of smartphones.
|    
*/
    goCallPhone: function(){
        window.location = 'tel:' + MajorProjectSenchaTouch.app.tplibHotline;
    },
    /*
|--------------------------------------------------------------------------
| goEmail (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function is triggered by tapping on the email button on the ContactUs view
| to access the native email function of smartphones.
|    
*/
    goEmail: function(){
        window.location = 'mailto:' + MajorProjectSenchaTouch.app.tplibEmail;
    },
    /*
|--------------------------------------------------------------------------
| goOpenLibraryBlog (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function is triggered by tapping on the blog button on the ContactUs view
| to open a new window to the TP Library Blog website.
|    
*/
    goOpenLibraryBlog: function(){
        window.open(MajorProjectSenchaTouch.app.tpliBlogUrl);
    },
    /*
|--------------------------------------------------------------------------
| goOpenLibraryFacebook (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function is triggered by tapping on the blog button on the ContactUs view
| to open a new window to the TP Library Facebook website.
|    
*/
    goOpenLibraryFacebook: function(){
        window.open(MajorProjectSenchaTouch.app.tplibFacebookUrl);
    },
    /*
|--------------------------------------------------------------------------
| goSubmitToEmail (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function is triggered by tapping on the submit button on the Recommend Title
| overlay to submit the form data to email.
|    
*/
    goSubmitToEmail: function(){
        var rtModel = Ext.create('MajorProjectSenchaTouch.model.RecommendTitle'),
            errors, errorMessage = '';
                                        
        var rtView = this.getRecommendTitleOverlay();    
        rtView.updateRecord(rtModel);
        errors = rtModel.validate();
                                    
        if(!errors.isValid()){
            errors.each(function (error) {
                errorMessage += error.getMessage() + '<br/>'; 
            });
            Ext.Msg.alert('Form is invalid!', errorMessage)
        }
        else{
            var name = Ext.getCmp('name').getValue();
            var email = Ext.getCmp('email').getValue();
            var departmentOrSchool = Ext.getCmp('departmentOrSchool').getValue();
            var recommendedTitle = Ext.getCmp('recommendedTitle').getValue();
            var otherInfoOnTitle = Ext.getCmp('otherInfoOnTitle').getValue();
            var titleType = Ext.getCmp('titleType').getValue();
            
            window.location = 'mailto:' + MajorProjectSenchaTouch.app.tplibRecommendTitleEmail + '?subject=RecommendedTitle: ' + recommendedTitle + 
                              '&body=Name: ' + name +
                              '%0AEmail: ' + email +
                              '%0ADepartment/School: ' + departmentOrSchool +
                              '%0AI recommend this Title: ' + recommendedTitle +
                              '%0ATitle is a: ' + titleType +
                              '%0AOther info on Title: ' + otherInfoOnTitle;
        }
    }
});